#include "timer_channel.h"
#include "./../loop_module/loop.h"
#include "timer_description.h"
#include <bits/types/struct_timeval.h>
#include <cstdint>
#include <cstdio>
#include <memory>
#include <utility>
#include <sys/select.h>

TimerChannel::TimerChannel(Loop * loop, std::shared_ptr<Description> description)
{
	m_loop = loop;
	m_description = std::move(description);
	m_index = -1;
}

void TimerChannel::Trigger(std::function<void(std::shared_ptr<ChannelProtocol>)> && function)
{
    m_trigger_func = function;
}

void TimerChannel::OnEvent(EventData & data)
{
    if (data.events & EmIoErr)
    {
    }
    else
    {
        if (data.events & EmIoIn)
        {
            if (m_trigger_func)
            {
                m_trigger_func(shared_from_this());
            }
        }
    }
}

int TimerChannel::AddEvent(int event)
{
    bool result = m_loop->AddEvent(shared_from_this(), event);
    return result == true;
}

int TimerChannel::ModEvent(int event)
{
    bool result = m_loop->ModEvent(m_index, event);
    return result == true;
}

int TimerChannel::DelEvent()
{
    bool result = m_loop->DelEvent(m_index);
    m_index     = -1;
    return result == true;
}

void TimerChannel::Index(int64_t index)
{
    m_index = index;
}

int64_t TimerChannel::Index()
{
    return m_index;
}

int TimerChannel::DescriptionData()
{
    return m_description->DescriptionData();
}
